$(document).ready(function(){
    document.getElementById('categoryAction').style.display = 'none';
    setTimeout("closeResultMessages()",5000);

    bkLib.onDomLoaded(function(){
        var addEditor = new nicEditor({
            fullPanel : true,
            maxHeight:500
        }).panelInstance('rbHtml');
        addEditor.addEvent("blur", function() {
            document.getElementById("rbHtml").value = addEditor.instanceById("rbHtml").getContent();
        });

        var editEditor = new nicEditor({
            fullPanel : true,
            maxHeight:500
        }).panelInstance('rbEditHtml');
        editEditor.addEvent("blur", function() {
            document.getElementById("rbEditHtml").value = editEditor.instanceById("rbEditHtml").getContent();
        });
    });

    $('input:radio[name=view_type]').change(function (){
        if($('input:radio[name=view_type]:checked').val() == 'html') {
            $('#divRelateBoxArticle').hide();
            $('#divRelateBoxHtml').show();
        }else if($('input:radio[name=view_type]:checked').val() == 'article'){
            $('#divRelateBoxHtml').hide();
            $('#divRelateBoxArticle').show();
        }
    })

    $('#rbSearchArticleSubmit').click(function(){
        var keyword = $('#rbKeyword').val();
        var dateFrom = $('#dateFrom').val();
        var dateTo = $('#dateTo').val();
        var cat_id = $('#rbCategoryId').val();

        var postData = "cat_id="+cat_id;
        postData = postData + "&keyword="+keyword;
        postData = postData + "&dateFrom="+dateFrom;
        postData = postData + "&dateTo="+dateTo;
        postData = postData + "&cat_id="+cat_id;
        postData = postData + "&categoryActionType=searchArticleRelateBox";
        $.ajax({
            url: '/'+ADMIN_SECTION+'/category',
            type: 'POST',
            dataType: 'json',
            data: postData,
            timeout: 1000,
            error: function(){
                alert('Network connection error!');
            },
            success: function(data){
                $("#rbSearchArticleResult").html(data.html);

            }
        });


    });
    

    $('#rbSelectArticle').click(function(){
        listbox_moveOptions('rbSearchArticleResult','rbSearchArticleSelect');
    });
    $('#rbRemoveArticle').click(function(){
        listbox_removeOptions('rbSearchArticleSelect');
    });

    $('#addCategorySubmit').click(function(){
        if($('#categoryname').val()==''){
            alert("Category name can not be empty!");
            $('#categoryname').focus();
            return false;
        }

        var checkViewType = $('input:radio[name=view_type]:checked').val();
        if(checkViewType == "html") {
            if($("#rbHtml").val()!='<br>'){
                if($('#box_title').val()==''){
                    alert("Box title can not be empty!");
                    $('#box_title').focus();
                    return false;
                }
            }
        }else if(checkViewType == "article"){
            var src = document.getElementById('rbSearchArticleSelect');
            var totalOptions = src.options.length;
            if(totalOptions>0){
                if($('#box_title').val()==''){
                    alert("Box title can not be empty!");
                    $('#box_title').focus();
                    return false;
                }

            }
        }
        listbox_selectall('rbSearchArticleSelect',true);
        document.formAddCategory.submit();
    });





    //edit form

    $('input:radio[name=edit_view_type]').change(function (){
        if($('input:radio[name=edit_view_type]:checked').val() == 'html') {
            $('#divEditRelateBoxArticle').hide();
            $('#divEditRelateBoxHtml').show();
        }else if($('input:radio[name=edit_view_type]:checked').val() == 'article'){
            $('#divEditRelateBoxHtml').hide();
            $('#divEditRelateBoxArticle').show();
        }
    })
    $('#rbEditSearchArticleSubmit').click(function(){
        var keyword = $('#rbEditKeyword').val();
        var dateFrom = $('#editDateFrom').val();
        var dateTo = $('#editDateTo').val();
        var cat_id = $('#rbEditCategoryId').val();

        var postData = "cat_id="+cat_id;
        postData = postData + "&keyword="+keyword;
        postData = postData + "&dateFrom="+dateFrom;
        postData = postData + "&dateTo="+dateTo;
        postData = postData + "&cat_id="+cat_id;
        postData = postData + "&categoryActionType=searchArticleRelateBox";
        $.ajax({
            url: '/'+ADMIN_SECTION+'/category',
            type: 'POST',
            dataType: 'json',
            data: postData,
            timeout: 1000,
            error: function(){
                alert('Network connection error!');
            },
            success: function(data){
                $("#rbEditSearchArticleResult").html(data.html);

            }
        });


    });
    $('#rbEditSelectArticle').click(function(){
        listbox_moveOptions('rbEditSearchArticleResult','rbEditSearchArticleSelect');
    });
    $('#rbEditRemoveArticle').click(function(){
        listbox_removeOptions('rbEditSearchArticleSelect');
    });

    $('#editCategorySubmit').click(function(){
        if($('#edCategoryname').val()==''){
            alert("Category name can not be empty!");
            $('#edCategoryname').focus();
            return false;
        }

        var checkViewType = $('input:radio[name=edit_view_type]:checked').val();
        if(checkViewType == "html") {
            if($("#rbEditHtml").val()!='<br>'){
                if($('#edit_box_title').val()==''){
                    alert("Box title can not be empty!");
                    $('#edit_box_title').focus();
                    return false;
                }
            }
        }else if(checkViewType == "article"){
            var src = document.getElementById('rbEditSearchArticleSelect');
            var totalOptions = src.options.length;
            if(totalOptions>0){
                if($('#edit_box_title').val()==''){
                    alert("Box title can not be empty!");
                    $('#edit_box_title').focus();
                    return false;
                }
            }
        }
        listbox_selectall('rbEditSearchArticleSelect',true);
        document.formEditCategory.submit();
    });
});


function addCategory(){
    document.getElementById('categoryAction').style.display = '';
    document.getElementById('formAddCategory').style.display = '';
    document.getElementById('formEditCategory').style.display = 'none';
    $("#categoryActionType").val('addCategory');
}
function editCategory(id){
    document.getElementById('categoryAction').style.display = '';
    document.getElementById('formAddCategory').style.display = 'none';
    document.getElementById('formEditCategory').style.display = '';
    $.ajax({
        url: '/'+ADMIN_SECTION+'/category',
        type: 'POST',
        dataType: 'json',
        data: "id="+id+"&categoryActionType=editCategory",
        timeout: 1000,
        error: function(){
            alert('Error loading category infomation');
        },
        success: function(data){
            $("#edId").val(data.id);
            $("#edParent_id").val(data.parent_id);
            $("#edCategoryname").val(data.name);
            $("#edCategoryname").val(data.name);
            if(data.cat_menu==1){
                $("#edCatMenu").attr('checked',true);
            }
            if(data.cat_typical==1){
                $("#edCatTypical").attr('checked',true);
            }
            if(data.cat_active==1){
                $("#edCatActive").attr('checked',true);
            }
            if(data.rbId>0){
                $("#rbId").val(data.rbId);
            }
            
            $("#rbEditActive").val(data.rbActive);
            
            if(data.rbTitle!=''){
                $("#edit_box_title").val(data.rbTitle);
            }

            if(data.rbViewType=='html'){
                $('#rbEditViewTypeArticle').attr('checked', false)
                $('#rbEditViewTypeHtml').attr('checked', true)
                $('#divEditRelateBoxArticle').hide();
                $('#divEditRelateBoxHtml').show();
                nicEditors.findEditor("rbEditHtml").setContent(data.rbValue);
            }else if(data.rbViewType=='article'){
                $('#rbEditViewTypeArticle').attr('checked', true)
                $('#rbEditViewTypeHtml').attr('checked', false)
                $('#divEditRelateBoxHtml').hide();
                $('#divEditRelateBoxArticle').show();
                $('#rbEditSearchArticleSelect').html(data.rbValue);
            }else if(typeof(data.rbViewType)=='undefined'){
                $('#rbEditViewTypeArticle').attr('checked', true)
                $('#rbEditViewTypeHtml').attr('checked', false)
                $('#divEditRelateBoxHtml').hide();
                $('#divEditRelateBoxArticle').show();
                $("#edit_box_title").val('');
                nicEditors.findEditor("rbEditHtml").setContent('');
                $("#rbEditSearchArticleResult").html('');
                $("#rbEditSearchArticleSelect").html('');
            }
            $("#categoryActionType").val('updateCategory');
        }
    });
}

function deleteCategory(id,txtAlert){
    if (confirm("Do you want to delete "+txtAlert+"?")){
        $.ajax({
            url: '/'+ADMIN_SECTION+'/category',
            type: 'POST',
            data: "id="+id+"&categoryActionType=deleteCategory",
            error: function(){
                alert('Error loading category infomation');
            },
            success: function(){
                window.location = "/"+ADMIN_SECTION+"/category";
            }
        });
    }
}

function closeResultMessages(){
    document.getElementById('rsMessages').style.display = 'none';
}
function closeHtml(id){
    document.getElementById(id).style.display = 'none';
}


String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function toggleMenu(id,obj){
    $.ajax({
        url: '/'+ADMIN_SECTION+'/category',
        type: 'POST',
        data: "id="+id+"&categoryActionType=toggleMenu",
        error: function(){
            alert('Error loading category infomation');
        },
        success: function(){
            if($(obj).html().trim().length==2){
                $(obj).html('Yes');
            }else if ($(obj).html().trim().length==3){
                $(obj).html("--");
            }
        }
    });
}
function toggleTypical(id,obj){
    $.ajax({
        url: '/'+ADMIN_SECTION+'/category',
        type: 'POST',
        data: "id="+id+"&categoryActionType=toggleTypical",
        error: function(){
            alert('Error loading category infomation');
        },
        success: function(){
            if($(obj).html().trim().length==2){
                $(obj).html('Yes');
            }else if ($(obj).html().trim().length==3){
                $(obj).html("--");
            }
        }
    });
}
function toggleActive(id,obj){
    $.ajax({
        url: '/'+ADMIN_SECTION+'/category',
        type: 'POST',
        data: "id="+id+"&categoryActionType=toggleActive",
        error: function(){
            alert('Error loading category infomation');
        },
        success: function(){
            if($(obj).html().trim().length==2){
                $(obj).html('Yes');
            }else if ($(obj).html().trim().length==3){
                $(obj).html("--");
            }
        }
    });
}